﻿using System;
using System.Web;
using System.Web.Mvc;

namespace Web.Infrastructure.Mvc.MvcCaptcha
{
    public static class HtmlHelperForCaptchaExtensions
    {
        public static IHtmlString TCaptcha<TModel>(this HtmlHelper<TModel> html) where TModel : CaptchaModel
        {
            var url = new UrlHelper(html.ViewContext.RequestContext).Content("~/Captcha?name=" + html.ViewData.Model.CaptchaName);
            var img = string.Format("<img class=\"captcha\" src=\"{0}\" />", url);
            return MvcHtmlString.Create(img);
        }

        public static IHtmlString Captcha(this HtmlHelper html)
        {
            var url = new UrlHelper(html.ViewContext.RequestContext).Content("~/Captcha?name="
                + (html.ViewData.ModelMetadata.Container as CaptchaModel).CaptchaName
                + "&unique=" + Guid.NewGuid().ToString().Replace("-", ""));
            var img = string.Format("<img class=\"captcha\" src=\"{0}\" />", url);
            return MvcHtmlString.Create(img);
        }
    }
}